<?php
/**
 * @author spenceryg
 * @date 2023/4/7
 * @time 4:54 下午
 * @focus love & life
 */


namespace app\common\lib;




use DfaFilter\SensitiveHelper;

class Sensitive
{
    /**
     * has
     * @param $word
     * @return bool
     * @throws \DfaFilter\Exceptions\PdsBusinessException
     * @throws \DfaFilter\Exceptions\PdsSystemException
     */
    public static function has($word): bool
    {
        $wordFilePath = app()->getRootPath() . 'public/word.txt';
        $handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);
        return $handle->islegal($word);
    }

    /**
     * replace
     * @param $word
     * @return \DfaFilter\文本内容|mixed
     * @throws \DfaFilter\Exceptions\PdsBusinessException
     * @throws \DfaFilter\Exceptions\PdsSystemException
     */
    public static function replace($word)
    {
        $wordFilePath = app()->getRootPath() . 'public/word.txt';
        $handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);
        return $handle->replace($word, '*', true);
    }

    public function get($word): string
    {
        $wordFilePath = app()->getRootPath() . 'public/word.txt';
        $handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);
        return  implode(',', $handle->getBadWord($word, 1));
    }
}
